{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3.9 \u5927\u578b\u6570\u7ec4\u8fd0\u7b97\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u5728\u5927\u6570\u636e\u96c6(\u6bd4\u5982\u6570\u7ec4\u6216\u7f51\u683c)\u4e0a\u9762\u6267\u884c\u8ba1\u7b97\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6d89\u53ca\u5230\u6570\u7ec4\u7684\u91cd\u91cf\u7ea7\u8fd0\u7b97\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528 NumPy \u5e93\u3002\nNumPy \u7684\u4e00\u4e2a\u4e3b\u8981\u7279\u5f81\u662f\u5b83\u4f1a\u7ed9Python\u63d0\u4f9b\u4e00\u4e2a\u6570\u7ec4\u5bf9\u8c61\uff0c\u76f8\u6bd4\u6807\u51c6\u7684Python\u5217\u8868\u800c\u5df2\u66f4\u9002\u5408\u7528\u6765\u505a\u6570\u5b66\u8fd0\u7b97\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5c0f\u4f8b\u5b50\uff0c\u5411\u4f60\u5c55\u793a\u6807\u51c6\u5217\u8868\u5bf9\u8c61\u548c NumPy \u6570\u7ec4\u5bf9\u8c61\u4e4b\u95f4\u7684\u5dee\u522b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Python lists\nx = [1, 2, 3, 4]\ny = [5, 6, 7, 8]\nx * 2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x + 10"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x + y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Numpy arrays\nimport numpy as np\nax = np.array([1, 2, 3, 4])\nay = np.array([5, 6, 7, 8])\nax * 2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "ax + 10"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "ax + ay"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "ax * ay"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u6240\u89c1\uff0c\u4e24\u79cd\u65b9\u6848\u4e2d\u6570\u7ec4\u7684\u57fa\u672c\u6570\u5b66\u8fd0\u7b97\u7ed3\u679c\u5e76\u4e0d\u76f8\u540c\u3002\n\u7279\u522b\u7684\uff0c NumPy \u4e2d\u7684\u6807\u91cf\u8fd0\u7b97(\u6bd4\u5982 ax * 2 \u6216 ax + 10 )\u4f1a\u4f5c\u7528\u5728\u6bcf\u4e00\u4e2a\u5143\u7d20\u4e0a\u3002\n\u53e6\u5916\uff0c\u5f53\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u6570\u7ec4\u7684\u65f6\u5019\u6267\u884c\u5143\u7d20\u5bf9\u7b49\u4f4d\u7f6e\u8ba1\u7b97\uff0c\u5e76\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u6574\u4e2a\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u540c\u65f6\u6267\u884c\u6570\u5b66\u8fd0\u7b97\u53ef\u4ee5\u4f7f\u5f97\u4f5c\u7528\u5728\u6574\u4e2a\u6570\u7ec4\u4e0a\u7684\u51fd\u6570\u8fd0\u7b97\u7b80\u5355\u800c\u53c8\u5feb\u901f\u3002\n\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8ba1\u7b97\u591a\u9879\u5f0f\u7684\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def f(x):\nreturn 3*x**2 - 2*x + 7\nf(ax)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "NumPy \u8fd8\u4e3a\u6570\u7ec4\u64cd\u4f5c\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u901a\u7528\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a math \u6a21\u5757\u4e2d\u7c7b\u4f3c\u51fd\u6570\u7684\u66ff\u4ee3\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "np.sqrt(ax)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "np.cos(ax)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u4e9b\u901a\u7528\u51fd\u6570\u8981\u6bd4\u5faa\u73af\u6570\u7ec4\u5e76\u4f7f\u7528 math \u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6267\u884c\u8ba1\u7b97\u8981\u5feb\u7684\u591a\u3002\n\u56e0\u6b64\uff0c\u53ea\u8981\u6709\u53ef\u80fd\u7684\u8bdd\u5c3d\u91cf\u9009\u62e9 NumPy \u7684\u6570\u7ec4\u65b9\u6848\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5e95\u5c42\u5b9e\u73b0\u4e2d\uff0c NumPy \u6570\u7ec4\u4f7f\u7528\u4e86C\u6216\u8005Fortran\u8bed\u8a00\u7684\u673a\u5236\u5206\u914d\u5185\u5b58\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u662f\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u8fde\u7eed\u7684\u5e76\u7531\u540c\u7c7b\u578b\u6570\u636e\u7ec4\u6210\u7684\u5185\u5b58\u533a\u57df\u3002\n\u6240\u4ee5\uff0c\u4f60\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u6bd4\u666e\u901aPython\u5217\u8868\u5927\u7684\u591a\u7684\u6570\u7ec4\u3002\n\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u60f3\u6784\u9020\u4e00\u4e2a10,000*10,000\u7684\u6d6e\u70b9\u6570\u4e8c\u7ef4\u7f51\u683c\uff0c\u5f88\u8f7b\u677e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "grid = np.zeros(shape=(10000,10000), dtype=float)\ngrid"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6240\u6709\u7684\u666e\u901a\u64cd\u4f5c\u8fd8\u662f\u4f1a\u540c\u65f6\u4f5c\u7528\u5728\u6240\u6709\u5143\u7d20\u4e0a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "grid += 10\ngrid"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "np.sin(grid)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8e NumPy \u6709\u4e00\u70b9\u9700\u8981\u7279\u522b\u7684\u4e3b\u610f\uff0c\u90a3\u5c31\u662f\u5b83\u6269\u5c55Python\u5217\u8868\u7684\u7d22\u5f15\u529f\u80fd - \u7279\u522b\u662f\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\u3002\n\u4e3a\u4e86\u8bf4\u660e\u6e05\u695a\uff0c\u5148\u6784\u9020\u4e00\u4e2a\u7b80\u5355\u7684\u4e8c\u7ef4\u6570\u7ec4\u5e76\u8bd5\u7740\u505a\u4e9b\u8bd5\u9a8c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])\na"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Select row 1\na[1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Select column 1\na[:,1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Select a subregion and change it\na[1:3, 1:3]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a[1:3, 1:3] += 10\na"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Broadcast a row vector across an operation on all rows\na + [100, 101, 102, 103]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Conditional assignment on an array\nnp.where(a < 10, a, 10)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "NumPy \u662fPython\u9886\u57df\u4e2d\u5f88\u591a\u79d1\u5b66\u4e0e\u5de5\u7a0b\u5e93\u7684\u57fa\u7840\uff0c\u540c\u65f6\u4e5f\u662f\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684\u6700\u5927\u6700\u590d\u6742\u7684\u6a21\u5757\u3002\n\u5373\u4fbf\u5982\u6b64\uff0c\u5728\u521a\u5f00\u59cb\u7684\u65f6\u5019\u901a\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50\u548c\u73a9\u5177\u7a0b\u5e8f\u4e5f\u80fd\u5e2e\u6211\u4eec\u5b8c\u6210\u4e00\u4e9b\u6709\u8da3\u7684\u4e8b\u60c5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6211\u4eec\u5bfc\u5165 NumPy \u6a21\u5757\u7684\u65f6\u5019\u4f1a\u4f7f\u7528\u8bed\u53e5 import numpy as np \u3002\n\u8fd9\u6837\u7684\u8bdd\u4f60\u5c31\u4e0d\u7528\u518d\u4f60\u7684\u7a0b\u5e8f\u91cc\u9762\u4e00\u904d\u904d\u7684\u6572\u5165 numpy \uff0c\u53ea\u9700\u8981\u8f93\u5165 np \u5c31\u884c\u4e86\uff0c\u8282\u7701\u4e86\u4e0d\u5c11\u65f6\u95f4\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u60f3\u83b7\u53d6\u66f4\u591a\u7684\u4fe1\u606f\uff0c\u4f60\u5f53\u7136\u5f97\u53bb NumPy \u5b98\u7f51\u901b\u901b\u4e86\uff0c\u7f51\u5740\u662f\uff1a http://www.numpy.org"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}